﻿#include "PowerUp.h"
//#include "Global.h"
#include "Resource.h"

PowerUp::PowerUp(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Object_State, int Direction) : Object(x, y)//, ID, Object_State)
{
	this->setIDObjType(ID);
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\item_32.png"), 125, 0);

	_xOld = x;
	_yOld = y - 32;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	veloc_x = 0;
	veloc_y = 0;
	accel_x = 0;
	accel_y = 0;


	isMushroom = true;
	isGravity = true;


	this->obj_state = Object_State;
	this->direction = Direction;
	if (this->getIDObjType() == 43)		//nam mang
	{
		if (obj_state == IS_IDLE)
		{
			p_sprite->setFrame(7,7);	//frame an
		} 
		else if (obj_state == IS_MOVING)
		{
			isMushroom = true;
			p_sprite->setFrame(1,1);
			if (direction == 1) //sang trai
			{
				veloc_x = -0.2;
			} 
			else if (direction == 2) //sang trai
			{ 
				veloc_x = 0.2;
			}

		}
	} 
	else if (this->getIDObjType() == 45)	//nam to, hoa
	{
		if (obj_state == IS_IDLE)
		{
			p_sprite->setFrame(7,7);	//frame an
		} 
		else if (obj_state == IS_MOVING)
		{
			isMushroom = true;
			p_sprite->setFrame(0,0);
			if (direction == 1) //sang trai
			{
				veloc_x = -0.2;
			} 
			else if (direction == 2) //sang trai
			{ 
				veloc_x = 0.2;
			}

		}
		else if (obj_state == IS_READY)
		{
			isMushroom = false;
			p_sprite->setFrame(2,2);
		}
	}

}

PowerUp::~PowerUp()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}

void PowerUp::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}

}



void PowerUp::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		//checkObjects(obj_list);
		break;
	case IS_ACTIVING:
		//Move(gameTime,staObjs,dynObjs);
		//pos_x += veloc_x * gameTime;

		gameTime *= 10;
		veloc_y -= accel_y * gameTime;
		pos_y += veloc_y * gameTime + 1.0f/2 * accel_y * gameTime * gameTime;
		if(pos_y > _yOld + 32)
			if(isMushroom == true)
			{
				if (this->getIDObjType() == 43)		//nam mang
				{
					p_sprite->setFrame(1,1);
				}
				else								//nam lon
				{
					p_sprite->setFrame(0,0);
				}

				changeState(IS_MOVING);
			}
			else
			{
				p_sprite->setFrame(2,2);
				changeState(IS_READY);
			}
			break;
	case IS_MOVING:
		move(obj_list, gameTime);
		break;
	case IS_READY:
		//Move(gameTime,staObjs,dynObjs);
		break;
	}


	p_sprite->update(gameTime);
}

void PowerUp::checkObjects(list<Object*>* obj_list)
{

	list<Object*>::iterator i;
	Object* obj;
	if(isCheckedContainer==false)
		for(i = obj_list->begin(); i != obj_list->end(); i++)
		{
			obj = *i;
			//if (obj->getIDObjType() == 21 || obj->getIDObjType() == 25) //gach:21, gach dau hoi: 25
			/*if(Collision::RectVsRect(this->GetBond(),obj->GetBond()))
			{
			container=obj;
			break;
			}*/
		}

		isCheckedContainer=true;
}

void PowerUp::move(list<Object*>* obj_list, float TPF)
{
	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	Object* obj;
	list<Object*>::iterator i;

	_xOld = pos_x;
	_yOld = pos_y;


	//Theo phương x
	pos_x += veloc_x * TPF;

	//Theo phương y
	if (isGravity == false)
	{
		accel_y = 0;
	}
	else
	{
		accel_y = GRAVITY;
	}

	veloc_y -= accel_y * TPF;
	pos_y += veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF;


	for (i = obj_list->begin(); i != obj_list->end(); i++)
	{
		obj = *i;
		if (obj->getObjectType() == OBJECT_LAND)
		{			
			Box box(this->_xOld,
				this->_yOld - this->height, 
				width, 
				height, 
				veloc_x * TPF, 
				veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF);
						
			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth(),
				obj->getHeight()
				);

			Box boxphase = Collision::GetSweptBroadphaseBox(box);
			if (Collision::AABBCheck(boxphase, block))
			{
				float normalx = 0;
				float normaly = 0;
				float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
				if (0 < collision_time && collision_time < 1)
				{
					isGravity = false;
					if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
					{
						//pos_x = _xOld + veloc_x * collision_time;
						
						
						
						//veloc_x = -veloc_x;
						if (this->_yOld - this->height < obj->getPosY())
						{
							pos_x = _xOld + veloc_x * collision_time;
							veloc_x = -veloc_x;
							//pos_y = _yOld;
						}
					} 
					else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
					{
						//pos_x = _xOld + veloc_x * collision_time;
						//pos_x = _xOld;
						
						if (this->_yOld - this->height < obj->getPosY())
						{
							pos_x = _xOld + veloc_x * collision_time;
							veloc_x = -veloc_x;
							//pos_y = _yOld;
						}
						//veloc_x = -veloc_x;
						
					}
					else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
					{
						
					}
					else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
					{


						pos_y = _yOld + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time + 0;
						//pos_y = _yOld;
						accel_y = 0;
						veloc_y = 0;
						
						
						
					}
					//isGravity = false;
					//if (this->pos_y - this->height <= obj->getPosY())
					//{
					//	isGravity = false;
					//	//pos_y = this->height + obj->getPosY() + 0.1;
					//	pos_y = _yOld;
					//}
					//else
					//{
					//	isGravity = true;
					//}



				}
				else
				{
					isGravity = true;	//de dong nay k thi nam di chuyen bi dinh trong luc
					/*if (this->pos_y - this->height <= obj->getPosY() + 1.0)
					{
					isGravity = false;						
					pos_y = this->height + obj->getPosY() + 1.0;
					}
					else
					{
					isGravity = true;
					}*/
				}
			}
			else
			{
				/*if (this->_yOld - this->height > obj->getPosY() + 1.5)
				{
				isGravity = true;

				}
				else
				isGravity = false;*/
				isGravity = true;
				
			}



		}
		else
		{					
			isGravity = true;
		}

	}


	_xOld = pos_x;
	_yOld = pos_y;

	if (pos_y <= 0)	//roi xuong ho
	{
		changeState(IS_REMOVE);
	}

	if (veloc_x >= 0)
	{

		direction = 2;
	} 
	else
	{

		direction = 1;
	}
}

void PowerUp::changeState(int state)
{
	obj_state = state;
	switch(obj_state)
	{
	case IS_ACTIVING:
		
		veloc_y = 1.0;
		accel_y = GRAVITY;
		break;

	case IS_MOVING:
		
		veloc_y = 0;
		if(veloc_x == 0.1)
			veloc_x = -0.15;
		else 
			veloc_x = 0.15;
		//veloc_x = -0.15;
		accel_y = GRAVITY;
		break;

	case IS_READY:
		
		pos_y = _yOld + 32;
		break;

	case IS_FLOWER:
		isMushroom = false;
		changeState(IS_ACTIVING);
		break;

	case IS_REMOVE:
		break;
	}
}

OBJECT_TYPE PowerUp::getObjectType()
{
	return OBJECT_ITEM;
}
